package org.douxiaoban.client.activity;


import org.douxiaoban.client.R;
import org.douxiaoban.client.adapter.MiniBlogAdapter;
import org.douxiaoban.client.adapter.MiniBlogAdapter.ClickCallback;
import org.douxiaoban.client.adapter.UsersAdapter;
import org.douxiaoban.client.api.DoubanList.DoubanListType;
import org.douxiaoban.client.api.MiniBlogList;
import org.douxiaoban.client.api.MiniBlogList.SendType;
import org.douxiaoban.client.api.UserInfoData;
import org.douxiaoban.client.api.UserList;
import org.douxiaoban.client.app.Assistant;
import org.douxiaoban.client.util.ThreadTool;
import org.douxiaoban.client.util.WorkspaceView;
import org.douxiaoban.client.util.WorkspaceView.ScrollToScreenCallback;
import org.douxiaoban.client.widget.PopupRecommend;
import org.douxiaoban.client.widget.TitleStyle;
import org.douxiaoban.data.MiniBlogItemData;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.mobclick.android.MobclickAgent;


public class UserHomeActivity extends BaseActivity
        implements ScrollToScreenCallback, ClickCallback {
	private WorkspaceView scrollView;
	private UserInfoData m_userInfo;
	private Handler mHandler;		
	private UserList mContacts;
//	private UserList mFriends;
	private MiniBlogList mUserMiniBlogs;
	
	private TitleStyle mTitle;
//	private Boolean m_bFirstMiniBlog;
	
	private String mUserId;
	private String mUserName;
	
	private boolean mIsUserInfoFirst;
	private boolean mOnlyContacts;
	
	private PopupRecommend mPopup;
	
	@Override
	public void onCreate(Bundle bundel){
		super.onCreate(bundel);
		initHandler();
		Bundle bundle = getIntent().getExtras();
		mIsUserInfoFirst = true;
//		if(null != bundle){
			mUserId= bundle.getString(Assistant.KEY_USER_ID);
			int index = bundle.getInt(Assistant.KEY_INDEX);
//			App.currentUserName = bundle.getString(Assistant.KEY_USER_NAME);			
//		}
		scrollView = new WorkspaceView(this, null);
		
		mPopup = new PopupRecommend(this);
		
		scrollView.setTouchSlop(32);
		 //scrollView.unlock();	
		LayoutInflater inflater = LayoutInflater.from(this);
				
		View viewListMb = inflater.inflate( R.layout.miniblog_home, null);
		
		MiniBlogAdapter adapterMiniBlog = new MiniBlogAdapter(this);
		UsersAdapter adapterContacts = new UsersAdapter(this);
		
		mUserMiniBlogs = 
			new MiniBlogList(this, adapterMiniBlog, 
					DoubanListType.USER_MINIBLOG, viewListMb, mUserId);

		mContacts = 
			new UserList(this, adapterContacts, DoubanListType.CONTACTS);
		mContacts.setUserId(mUserId);
		adapterMiniBlog.setListView(mUserMiniBlogs.getListView());
		adapterMiniBlog.setList(mUserMiniBlogs);
		
	    requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
	    setContentView(scrollView);	
	    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
	    						  R.layout.custom_title);
	    
        mTitle = new TitleStyle(this);
        mTitle.initTitleStyle();
        
        adapterContacts.setList(mContacts);
        
       //from the btn friends 
		if(Assistant.HOME_INDEX_CONTACTS != index){
			scrollView.addView(View.inflate(this, R.layout.user_info, null));
			scrollView.addView(viewListMb); 	//广播列表
		    initCollectionListent();
			ThreadTool.instace(this).getUserInfo(mUserId, mHandler);
		    mTitle.initPageController(3,1);
		    mTitle.setTitle(getString(R.string.titleUserHome));
		    mOnlyContacts = false;
		}else{
			mOnlyContacts = true;
		    mTitle.initPageController(1,1);
		    mTitle.setTitle(getString(R.string.titleContacts));
		    mContacts.refresh();
		}
		scrollView.addView(mContacts.getMainview()); 	//关注列表
		mContacts.getListView().setAdapter(mContacts.getAdapter());
		
		//参数待定...
	
		scrollView.setScrollToScreenCallback(this);
//        callback(index);
//        scrollView.scrollToScreen(index);
	}
	
	
	private void initHandler(){
		mHandler = new Handler(){
			@Override
			public void handleMessage(Message msg){
				Bundle bundle = msg.getData();
				switch(msg.what){
					case Assistant.TOOLS_MSG_USINFO:
						ThreadTool.UserInfoSerialz serialz =
							(ThreadTool.UserInfoSerialz)bundle.getSerializable(Assistant.TOOLS_KEY_USER_INFO);
					    m_userInfo = serialz.m_userInfo;
					    if(null == m_userInfo){
					    	mPopup.showToastWindow(Assistant.FAIL_POP, "网络异常");
					    }else{
						    updateUserInfo();
					    }
					    break;
					    
					case Assistant.TOOLS_MSG_MINIBLOG:
						break;
						
					case Assistant.TOOLS_MSG_AVATAR:
						ThreadTool.ImageSerialz image =
								(ThreadTool.ImageSerialz)bundle.getSerializable(Assistant.TOOLS_KEY_AVATAR);
						ImageView avatar = (ImageView)findViewById(R.id.userInfo_avatar);
						avatar.setImageBitmap(image.m_bitmap);
					default:
						break;
				}
			}
		};
	}
	
	
	private void updateUserInfo(){
		intiUserInfoUi();
	}
	
	private void intiUserInfoUi(){
		TextView name = (TextView)findViewById(R.id.userInfo_name);
		TextView location = (TextView)findViewById(R.id.userInfo_location);
		TextView summary = (TextView)findViewById(R.id.userInfo_summary);
		TextView signature = (TextView)findViewById(R.id.userInfo_signatrue);
		
		mUserName = m_userInfo.mName;
		name.setText(m_userInfo.mName);
		signature.setText(m_userInfo.mSignature);
		location.setText(m_userInfo.mLocation);
		summary.setText(m_userInfo.mSummary);
		m_userInfo.mImageUrl = m_userInfo.mImageUrl.replace("/u", "/ul");
		ThreadTool.instace(this).
				getAvatar(m_userInfo.mId, m_userInfo.mImageUrl,mHandler, 0);
		//}
	}
	
	
	@Override
	public void callback(int index){
		
		if(mOnlyContacts){
			return;
		}
		mTitle.setCurrentPage(index+1);
		switch(index){
			case Assistant.HOME_INDEX_INFO:
				if(mIsUserInfoFirst){
					ThreadTool.instace(this).getUserInfo(mUserId, mHandler);
				}
				mTitle.setTitle(getString(R.string.titleUserHome));
				break;
			case Assistant.HOME_INDEX_MINIBLOG:
				mTitle.setTitle(getString(R.string.titleUserMiniBlog));
				if(mUserMiniBlogs.getIsFirst()){
					mUserMiniBlogs.refresh();
				}
				break;
			case Assistant.HOME_INDEX_CONTACTS:
				mTitle.setTitle(getString(R.string.titleContacts));
				if(mContacts.getIsFirst()){		
					mContacts.refresh();
				}
				break;
			case Assistant.HOME_INDEX_FRIEND:
				break;
				default:
					break;
		
		}
	}
	
	private  void changeCurrentUser(String userName, String userId){
		mUserId = userId;
		reset();		
	}
	/**
	 * 监听 豆邮,和收藏
	 */
	private void initCollectionListent(){
		TextView book = (TextView)findViewById(R.id.userInfoBook);
		TextView mail = (TextView)findViewById(R.id.userInfoSendMail);
		TextView movie = (TextView)findViewById(R.id.userInfoMovie);
		TextView music = (TextView)findViewById(R.id.userInfoMusic);
		
		book.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				actionToBook();
			}
		});
		
		mail.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				actionToSendMail();
			}
		});
		
		movie.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				actionToMovie();
			}
		});
		
		music.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v){
				actionToMusic();
			}
		});
	}
	
	private void actionToBook(){
		Intent toBook = 
			new Intent(UserHomeActivity.this, 
					   CollectionBookListActivity.class);
		
		toBook.putExtra(Assistant.KEY_USER_ID, mUserId);
		startActivity(toBook);
	}
	
	private void actionToSendMail(){
		Intent toSendMail = 
			new Intent(UserHomeActivity.this, SendMailActivity.class);
		toSendMail.putExtra(Assistant.KEY_USER_ID, mUserId);
		toSendMail.putExtra(Assistant.KEY_USER_NAME, mUserName);
		startActivity(toSendMail);
	}
	
	private void actionToMovie(){
		Intent toMovie = 
			new Intent(UserHomeActivity.this, CollectionMovieListActivity.class);
		toMovie.putExtra(Assistant.KEY_USER_ID, mUserId);
		startActivity(toMovie);
	}
	
	private void actionToMusic(){
		Intent toMusic = 
			new Intent(UserHomeActivity.this, CollectionMusicListActivity.class);
		toMusic.putExtra(Assistant.KEY_USER_ID, mUserId);
		startActivity(toMusic);
	}
	/**
	 *  切换用户的时候,重置数据..
	 */
	private void reset(){
		mContacts.reset();
	}
	
	@Override
	public void revBtnClick(String title, SendType type, String str) {
		// TODO Auto-generated method stub
		mUserMiniBlogs.revBtnClick(title, type, str);
	}

	@Override
	public void itemClick(MiniBlogItemData data) {
//		mUserMiniBlogs.clickEvent(data);
	}
	
	@Override
	public void avatarClick(String userName, String userId) {
		// TODO Auto-generated method stub


	}

  

}